﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using SCMS.Model;
using SCMS.CoreBusinessLogic.OrderRequest;
using SCMS.Resource;

namespace SCMS.UI.Models
{
    public class ORequest
    {
        public SCMS.Model.OrderRequest EntityOrderRequest { get; set; }

        [Required]
        [DataType(DataType.Text)]
        public string OrderRequestNo { get; set; }

        // Stores the selected value from the drop down box.
        [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Global_String_Required")]
        public Guid CurrencyID
        {
            get;
            set;
        }
        public SelectList currencies { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Global_String_Required")]
        public Guid ProjectId { get; set; }
        public SelectList Projects { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Global_String_Required")]
        public Guid? ProjectDonorId { get; set; }
        public SelectList ProjectNos { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Global_String_Required")]
        public Guid RequestDelivLocationID
        {
            get;
            set;
        }
        public SelectList RequestDelivLocations { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Global_String_Required")]
        public Guid FinalDelivLocationID
        {
            get;
            set;
        }
        public SelectList FinalDelivLocations { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Global_String_Required")]
        [DataType(DataType.Date)]
        public DateTime? RequestDate
        {
            get;
            set;
        }

        [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Global_String_Required")]
        [DataType(DataType.Date)]
        public DateTime? IssueDate
        {
            get;
            set;
        }

        public bool FromPP { get; set; }
    }

    public class ORequester
    {
        public SCMS.Model.OrderRequest EntityOrderRequest { get; set; }

        [Required]
        [DataType(DataType.Text)]
        public string OrderRequestNo
        {
            get { return EntityOrderRequest.RefNumber; }
            set { EntityOrderRequest.RefNumber = value; }
        }

        // Stores the selected value from the drop down box.
        [Required]
        public Guid CurrencyID
        {
            get { return EntityOrderRequest.CurrencyId; }
            set { EntityOrderRequest.CurrencyId = value; }
        }
        public SelectList currencies { get; set; }

        public SelectList Projects { get; set; }
        public SelectList ProjectNos { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Global_String_Required")]
        public Guid? RequestDelivLocationID
        {
            get { return EntityOrderRequest.RequestedDestinationId; }
            set { EntityOrderRequest.RequestedDestinationId = value; }
        }
        public SelectList RequestDelivLocations { get; set; }

        [Required]
        public Guid? FinalDelivLocationID
        {
            get { return EntityOrderRequest.FinalDestinationId; }
            set { EntityOrderRequest.FinalDestinationId = value; }
        }
        public SelectList FinalDelivLocations { get; set; }

        [Required]
        [DataType(DataType.Date)]
        public DateTime RequestDate
        {
            get { return (DateTime)EntityOrderRequest.DeliveryDate; }
            set { EntityOrderRequest.DeliveryDate = value; }
        }

        [Required]
        [DataType(DataType.Date)]
        public DateTime IssueDate
        {
            get { return (DateTime)EntityOrderRequest.OrderDate; }
            set { EntityOrderRequest.OrderDate = value; }
        }
    }

    public class LoadORDetailsz
    {
        public SCMS.Model.OrderRequestItem EntityOrderRequestItem { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Global_String_Required")]
        public string ItemName
        {
            get { return EntityOrderRequestItem.Item.Name; }
            set { EntityOrderRequestItem.Item.Name = value; }
        }

        [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Global_String_Required")]
        public string UnitMessureID
        {
            get { return EntityOrderRequestItem.Item.UnitOfMeasure.Code; }
            set { EntityOrderRequestItem.Item.UnitOfMeasure.Code = value; }
        }
        public SelectList UnitMessures { get; set; }


        [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Global_String_Required")]
        public decimal Quantity
        {
            get { return EntityOrderRequestItem.Quantity; }
            set { EntityOrderRequestItem.Quantity = value; }
        }


        [DataType(DataType.Currency)]
        [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Global_String_Required")]
        public decimal UnitpPrice
        {
            get { return EntityOrderRequestItem.EstimatedUnitPrice; }
            set { EntityOrderRequestItem.EstimatedUnitPrice = value; }
        }

        [DataType(DataType.Currency)]
        [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Global_String_Required")]
        public decimal TotalPrice
        {
            get { return EntityOrderRequestItem.EstimatedPrice; }
            set { EntityOrderRequestItem.EstimatedPrice = value; }
        }
        
        public string ProjectNo
        {
            get;
            set;
        }

        public string BudgetLine
        {
            get;
            set;
        }


        [DataType(DataType.Text)]
        public string Remarks
        {
            get { return EntityOrderRequestItem.Remarks; }
            set { EntityOrderRequestItem.Remarks = value; }
        }

        [Key]
        public Guid OrderRequestID
        {
            get { return (Guid)EntityOrderRequestItem.Id; }
            set { EntityOrderRequestItem.Id = value; }
        }
    }

    public class ChildtRequest
    {
        public SCMS.Model.OrderRequestItem EntityOrderRequestItem { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Global_String_Required")]
        public Guid ItemID
        {
            get { return EntityOrderRequestItem.ItemId; }
            set { EntityOrderRequestItem.ItemId = value; }
        }
        public SelectList Items { get; set; }


        [DataType(DataType.Text)]
        [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Global_String_Required")]
        public string Description
        {
            get { return EntityOrderRequestItem.ItemDescription; }
            set { EntityOrderRequestItem.ItemDescription = value; }
        }

        [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Global_String_Required")]
        public decimal Quantity
        {
            get { return EntityOrderRequestItem.Quantity; }
            set { EntityOrderRequestItem.Quantity = value; }
        }

        [DataType(DataType.Currency)]
        [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Global_String_Required")]
        public decimal UnitpPrice
        {
            get { return EntityOrderRequestItem.EstimatedUnitPrice; }
            set { EntityOrderRequestItem.EstimatedUnitPrice = value; }
        }

        [DataType(DataType.Currency)]
        [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Global_String_Required")]
        public decimal TotalPrice
        {
            get { return EntityOrderRequestItem.EstimatedPrice; }
            set { EntityOrderRequestItem.EstimatedPrice = value; }
        }

        [Required]
        public Guid BudgetLineID { get; set; }
        public SelectList BudgetLines { get; set; }


        [DataType(DataType.Text)]
        public string Remarks
        {
            get { return EntityOrderRequestItem.Remarks; }
            set { EntityOrderRequestItem.Remarks = value; }
        }

        [Key]
        public Guid RequestItemID { get; set; }

        public Guid OrderRequestItemId
        {
            get { return EntityOrderRequestItem.Id; }
            set { EntityOrderRequestItem.Id = value; }
        }
    }

    public class ViewOR
    {
        public Model.OrderRequest EntityOR { get; set; }

        public string ORRefNo { get { return EntityOR.RefNumber; } set { EntityOR.RefNumber = value; } }

        public string curency { get; set; }

        public string Donor { get; set; }

        [DataType(DataType.Date)]
        public DateTime IssueDate { get; set; }

        public DateTime PreparedOn { get; set; }

        [DataType(DataType.Date)]
        public DateTime RequestDate { get; set; }

        public string RDD { get; set; }

        public string FDD { get; set; }

        [DisplayFormat(DataFormatString = "{0:#,##0.00;(#,##0.00);0}")]
        public decimal TotalCost { get; set; }

        [DisplayFormat(DataFormatString = "{0:#,##0.00;(#,##0.00);0}")]
        public decimal? MBValue { get; set; }
        public string project { get; set; }

        public Model.Staff PreparedBy { get; set; }

        public Model.Staff ReviewdBy { get; set; }

        [DataType(DataType.Date)]
        public DateTime ReveiwDate { get; set; }


        public Model.Staff AuthorizdBy { get; set; }

        [DataType(DataType.Date)]
        public DateTime AuthorizDate { get; set; }


        public string FirstItem { get; set; }

        public string ProjNo { get; set; }

        public string ProjName { get; set; }

        [Key]
        public Guid ORID { get { return EntityOR.Id; } set { EntityOR.Id = value; } }

        public List<ViewORDetails> ORDetails { get; set; }

        public bool CanEdit { get; set; }

        /// <summary>
        /// This applies to approve/finance review/authorize
        /// </summary>
        public bool CanApprove { get; set; }
        /// <summary>
        /// This applies to funds available before submit/approval/review/authroization
        /// </summary>
        public List<BudgetCheckResult> BudgetCheckResults { get; set; }
    }

    public class ViewORDetails
    {

        public Model.OrderRequestItem EntityORDetails { get; set; }

        public string ItemName { get { return EntityORDetails.Item.Name; } set { EntityORDetails.Item.Name = value; } }

        public string UnityMessure { get { return EntityORDetails.Item.UnitOfMeasure.Code; } set { EntityORDetails.Item.UnitOfMeasure.Code = value; } }

        [DisplayFormat(DataFormatString = "{0:#,##0.00;(#,##0.00);0}")]
        public decimal Quantity { get { return EntityORDetails.Quantity; } set { EntityORDetails.Quantity = value; } }

        [DisplayFormat(DataFormatString = "{0:#,##0.00;(#,##0.00);0}")]
        public decimal EstmatedUnityPrice { get { return EntityORDetails.EstimatedUnitPrice; } set { EntityORDetails.EstimatedUnitPrice = value; } }

        [DisplayFormat(DataFormatString = "{0:#,##0.00;(#,##0.00);0}")]
        public decimal EstmatedTotalPrice { get { return EntityORDetails.EstimatedPrice; } set { EntityORDetails.EstimatedPrice = value; } }

        public string Remarks { get { return EntityORDetails.Remarks; } set { EntityORDetails.Remarks = value; } }

        public string ProjectNo { get; set; }

        public string BudgetLine { get; set; }

        [Key]
        public Guid ORItemID { get { return EntityORDetails.Id; } set { EntityORDetails.Id = value; } }

    }

    public class Search
    {
        public Model.OrderRequest EntityOrderRequest { get; set; }

        public string ORNumber { get { return EntityOrderRequest.RefNumber; } set { EntityOrderRequest.RefNumber = value; } }

    }

    public class RejectOR
    {
        [Required]
        public Guid Id { get; set; }

        public string RefNumber { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Global_String_Required")]
        public string RejectedAuthRemarks { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Global_String_Required")]
        public string RejectedReviewRemarks { get; set; }

        public bool IsReview { get; set; }
    }

    public class ORExtension
    {
        public static ViewOR PrepareOR(IOrderRequest orderRequestService, Guid orId, Guid currentStaffId)
        {
            decimal total = 0;
            UI.Models.ViewOR OR;
            UI.Models.ViewORDetails ORD;
            List<Models.ViewORDetails> mm = new List<ViewORDetails>();
            using (var db = new SCMSEntities())
            {
                Model.OrderRequest ORRqst = db.OrderRequests.SingleOrDefault(p => p.Id == orId);
                Currency mbCurr = db.Currencies.SingleOrDefault(c => c.Id == ORRqst.MBCurrencyId);
                var pd = ORRqst.ProjectDonor;
                //if (mbCurr != null)
                //    ViewBag.MBCurrency = mbCurr.ShortName;
                OR = new ViewOR();
                Model.ProjectDonor pddd = ORRqst.ProjectDonor;
                Model.Project pro = pddd.Project;
                OR.EntityOR = ORRqst;
                OR.PreparedBy = db.Staffs.SingleOrDefault(p => p.Id == ORRqst.PreparedBy);
                Model.Person ps = OR.PreparedBy.Person; Designation d = OR.PreparedBy.Designation;
                OR.IssueDate = ORRqst.OrderDate.Value;
                OR.PreparedOn = ORRqst.PreparedOn.Value;
                if ((bool)ORRqst.IsReviewed)
                {
                    OR.ReviewdBy = db.Staffs.SingleOrDefault(p => p.Id == ORRqst.ReviewedBy);
                    var desg = OR.ReviewdBy.Designation;
                    var person = OR.ReviewdBy.Person;
                    OR.ReveiwDate = ORRqst.ReviewedOn.Value;
                }
                if ((bool)ORRqst.IsAuthorized)
                {
                    OR.AuthorizdBy = db.Staffs.SingleOrDefault(p => p.Id == ORRqst.AuthorizedBy);
                    var desg = OR.AuthorizdBy.Designation;
                    var person = OR.AuthorizdBy.Person;
                    OR.AuthorizDate = ORRqst.AuthorizedOn.Value;
                }
                if ((bool)ORRqst.IsApproved)
                {
                    var desg = ORRqst.Staff.Designation;
                    var person = ORRqst.Staff.Person;
                }
                OR.RequestDate = ORRqst.DeliveryDate.Value;
                OR.curency = db.Currencies.SingleOrDefault(p => p.Id == ORRqst.CurrencyId).ShortName;
                OR.Donor = db.ProjectDonors.SingleOrDefault(p => p.Id == ORRqst.ProjectDonorId).Donor.ShortName;
                OR.RDD = db.Locations.SingleOrDefault(p => p.Id == ORRqst.RequestedDestinationId).Name;
                OR.FDD = db.Locations.SingleOrDefault(p => p.Id == ORRqst.FinalDestinationId).Name;
                OR.project = db.ProjectDonors.SingleOrDefault(p => p.Id == ORRqst.ProjectDonorId).Project.Name;
                List<Model.OrderRequestItem> ORItemsList = orderRequestService.GetOrderRequestItems(orId);

                foreach (Model.OrderRequestItem item in ORItemsList)
                {
                    total += item.EstimatedPrice;
                    ORD = new ViewORDetails();
                    ORD.EntityORDetails = item;
                    Item it = item.Item;
                    ORD.ProjectNo = orderRequestService.GetProjectDonorDetails((Guid)item.OrderRequest.ProjectDonorId).ProjectNumber;
                    ORD.BudgetLine = orderRequestService.GetProjectBudgetDetails((Guid)item.BudgetLineId).LineNumber;
                    mm.Add(ORD);
                }
                OR.TotalCost = total;
                OR.MBValue = ORRqst.MBValue;
                OR.ORDetails = mm;
                OR.CanEdit = ((ORRqst.IsSubmitted != true && ORRqst.IsRejected != true && ORRqst.Staff2.Id == currentStaffId) ||
                    (ORRqst.IsRejected == true && ORRqst.Staff != null && ORRqst.Staff.Id == currentStaffId) ||
                    (ORRqst.IsSubmitted && ORRqst.IsRejected == true && ORRqst.Staff == null && ORRqst.Staff2.Id == currentStaffId))
                    && (ORRqst.IsReviewed == false);              
                return OR;
            }
        }
    }
}